// 对axios进行二次封装
import axios from 'axios';
import { getCookie } from "@/utils/auth.js";
import router from '@/router'
let service = axios.create({
    // 基本地址
    baseURL: 'http://localhost:3000/api',
    // 超时时间
    timeout: 5000
});

// 请求拦截
service.interceptors.request.use(config => {
    // 获取cookie
    let token = getCookie();
    console.log(token);
    console.log(config);
    config.headers["hg_token"] = token;
    return config
})

// 响应拦截

service.interceptors.response.use(response => {
    // 所有响应都得先到响应拦截来打卡
    let status = response.data.status;
    switch (status) {
        case 200:
            return response
        case 1001:
        case 1002:
        case 1003:
        case 1004:
        case 1005:
            return response
        case 401:
            router.push('/login')
        default:
            break;
    }

})




// service此时就相当于添加了baseURL和timeout的axios方法
export default service;